package com.kinghood.productcenter.controller.restcloud;

import com.kinghood.infrastructure.model.KingHoodExceptionUtil;
import com.kinghood.infrastructure.model.KingHoodResponseVO;
import com.kinghood.infrastructure.model.principal.KingHoodUser;
import com.kinghood.infrastructure.util.UserUtil;
import com.kinghood.productcenter.common.model.dto.common.IdReqDTO;
import com.kinghood.productcenter.common.model.dto.product.ProductDTO;
import com.kinghood.productcenter.common.model.dto.restcloud.GetProductInfoReqDTO;
import com.kinghood.productcenter.manager.IRestCloudManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * description
 *
 * @author zhangFanJun
 * @date 2024-01-29 10:47
 **/
@RequiredArgsConstructor
@RequestMapping("/restCloud")
@Slf4j
@RestController
public class RestCloudProductController {

    private final IRestCloudManager restCloudManager;

    /**
     * 发布消息
     *
     * @param in 入参
     * @return 商品信息
     * @author zfj
     * @date 2023/12/25
     */
    @PostMapping("/pushProduct")
    public KingHoodResponseVO<Void> pushProduct(@RequestBody IdReqDTO in) {

        KingHoodUser user = UserUtil.getUser();
        KingHoodExceptionUtil.trueThrows(Objects.isNull(user)||!Objects.equals(user.getIssuer(),"open"),"用户没有操作权限");
        restCloudManager.pushProduct(in);
        return KingHoodResponseVO.success();
    }

    /**
     * 消费
     *
     * @param in 入参
     * @return 商品信息
     * @author zfj
     * @date 2023/12/25
     */
    @PostMapping("/getProduct")
    public KingHoodResponseVO<ProductDTO> getProduct(@RequestBody ProductDTO in) {

        log.info("消费集成平台的数据：{}", in);
        return KingHoodResponseVO.success(in);
    }

    /**
     * 拉取数据
     *
     * @param in 入参
     * @return 商品信息
     * @author zfj
     * @date 2023/12/25
     */
    @PostMapping("/getProductInfo")
    public KingHoodResponseVO<List<ProductDTO>> getProductInfo(@RequestBody GetProductInfoReqDTO in) {

        KingHoodUser user = UserUtil.getUser();
        KingHoodExceptionUtil.trueThrows(Objects.isNull(user)||!Objects.equals(user.getIssuer(),"open"),"用户没有操作权限");
        return KingHoodResponseVO.success(restCloudManager.getProductInfo(in));
    }

}
